package com.qf.mybatisDemo_03.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author 刘欣
 * @createTime 2022/12/22 20:30
 * @description
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Book {
    private Integer id;
    private String name;
    private String author;
    private String publish;
    private Integer sort;

    public String toCvsStr() {
        return this.getName() + "," + this.getAuthor() + "," + this.getPublish() + "," + this.getSort();
    }

    public static Book cvsToBook(String cvs) throws Exception {
        Book book = new Book();
        if (cvs != null) {
            String[] split = cvs.split(",");
            if (split.length > 0) {
                book.setName(split[0]);
                book.setAuthor(split[1]);
                book.setPublish(split[2]);
                book.setSort(Integer.valueOf(split[3]));
            } else {
                System.out.println("该行无数据！");
            }
        } else {
            throw new Exception("cvs字符串为空");
        }
        return book;
    }

    @Override
    public String toString() {
        return this.getName() + "," + this.getAuthor() + "," + this.getPublish() + "," + this.getSort();
    }
}
